using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using 高校员工餐饮信息管理系统.Services;

namespace 高校员工餐饮信息管理系统.Controllers
{
    /// <summary>
    /// 商户控制器
    /// 演示服务层的数据查询和导出功能
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    [Authorize(Roles = "商户,系统管理员")]
    public class MerchantController : ControllerBase
    {
        private readonly IMerchantService _merchantService;
        private readonly ILogger<MerchantController> _logger;

        public MerchantController(IMerchantService merchantService, ILogger<MerchantController> logger)
        {
            _merchantService = merchantService;
            _logger = logger;
        }

        /// <summary>
        /// 获取所有商户
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetAllMerchants()
        {
            _logger.LogInformation("获取所有商户");

            var merchants = await _merchantService.GetAllMerchantsAsync();

            return Ok(new
            {
                Message = "查询成功",
                Count = merchants.Count,
                Merchants = merchants
            });
        }

        /// <summary>
        /// 获取商户信息
        /// </summary>
        [HttpGet("{merchantId}")]
        public async Task<IActionResult> GetMerchantById(int merchantId)
        {
            _logger.LogInformation($"获取商户信息: {merchantId}");

            var merchant = await _merchantService.GetMerchantByIdAsync(merchantId);

            if (merchant == null)
            {
                return NotFound(new { Message = "商户不存在" });
            }

            return Ok(new
            {
                Message = "查询成功",
                Merchant = merchant
            });
        }

        /// <summary>
        /// 查询商户订单
        /// </summary>
        [HttpGet("{merchantId}/orders")]
        public async Task<IActionResult> GetMerchantOrders(
            int merchantId,
            [FromQuery] DateTime? startDate,
            [FromQuery] DateTime? endDate)
        {
            var start = startDate ?? DateTime.Now.AddDays(-30);
            var end = endDate ?? DateTime.Now;

            _logger.LogInformation($"查询商户订单: {merchantId}, {start:yyyy-MM-dd} ~ {end:yyyy-MM-dd}");

            var orders = await _merchantService.GetMerchantOrdersAsync(merchantId, start, end);

            return Ok(new
            {
                Message = "查询成功",
                Count = orders.Count,
                Orders = orders
            });
        }

        /// <summary>
        /// 导出商户结算数据
        /// </summary>
        [HttpGet("{merchantId}/export")]
        public async Task<IActionResult> ExportSettlementData(
            int merchantId,
            [FromQuery] DateTime? startDate,
            [FromQuery] DateTime? endDate)
        {
            var start = startDate ?? DateTime.Now.AddDays(-30);
            var end = endDate ?? DateTime.Now;

            _logger.LogInformation($"导出商户结算数据: {merchantId}, {start:yyyy-MM-dd} ~ {end:yyyy-MM-dd}");

            var data = await _merchantService.ExportMerchantSettlementDataAsync(merchantId, start, end);

            return File(
                data, 
                "text/csv", 
                $"商户结算数据_{merchantId}_{start:yyyyMMdd}_{end:yyyyMMdd}.csv");
        }
    }
}

